package com.redis.config;

import com.redis.consumer.RedisReceiver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * @Author: 胡成
 * @Version: 0.0.1V
 * @Date: 2018/10/26
 * @Description: 类描述
 **/
@Configuration
public class RedisSubListenerConfig {

    /**
     * @Author: 胡成
     * @Date: 2018/10/26 9:29
     * @Description: 初始化监听器
     **/
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory factory, MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
        listenerContainer.setConnectionFactory(factory);
        listenerContainer.addMessageListener(listenerAdapter,new PatternTopic("order1"));
        return listenerContainer;
    }

    /**
     * @Author: 胡成
     * @Date: 2018/10/26 9:36
     * @Description: 利用反射来创建监听到消息后执行的方法
    **/
    @Bean
    public MessageListenerAdapter listenerAdapter(RedisReceiver redisReceiver){
        return new MessageListenerAdapter(redisReceiver,"receiveMessage");
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory){
        return new StringRedisTemplate(connectionFactory);
    }



}
